<?php
class Admin_SectorController extends Protox_Controller_Action {

	public function indexAction() {
		$this->_helper->redirector('list', 'sector', 'admin');
	}

	public function listAction() {
		$this->view->pageTitle = 'Gerenciar setores';

		$sectorsDbTable = new Admin_Model_Sectors();
		$organsDbTable = new Admin_Model_Organs();

		$form = new Admin_Form_SectorSearch();
		$request = $this->getRequest();

		$this->view->form = $form;

		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$name = strtolower($form->getValue('name'));
				$acronym = strtolower($form->getValue('acronym'));
				$organ = $form->getValue('id_organ');
				$sector = $form->getValue('id_sector');

				$id_organ = null;
				if ($organ) {
					$organ = $organsDbTable->getOrganByName($organ);
					if ($organ['id']) {
						$id_organ = $organ['id'];
						$form->getElement('id_organ')->setValue($id_organ);
					}
				}
				$id_sector = null;
				if ($sector) {
					$sector = substr($sector,strpos($sector,'-')+1,strlen($sector));
					$sector = $sectorsDbTable->getSectorByName($sector);
					if ($sector['id']) {
						$id_sector = $sector['id'];
						$form->getElement('id_sector')->setValue($id_sector);
					}
				}

				$select = $sectorsDbTable->findSectors($name, $acronym, $id_organ, $id_sector);
				if (sizeOf($select) == 0) {
					$this->view->priorityMessenger(sprintf('Nenhum setor encontrado'), 'alert');
				}
			}
		} else {
			$select = $sectorsDbTable->getSectors();
		}

		$paginator = Zend_Paginator :: factory($select);
		$page = $this->_getParam('page', 1);
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage(20);
		
		$this->view->total = count($select);
		$this->view->paginator = $paginator;
	}

	public function addAction() {
		$this->view->pageTitle = 'Inserir novo setor';

		$form = new Admin_Form_SectorAdd();
		$request = $this->getRequest();

		$this->view->form = $form;

		try {
			if ($request->isPost()) {
				if ($form->isValid($request->getPost())) {
					$name = $form->getValue('name');
					$acronym = $form->getValue('acronym');
					$organ = $form->getValue('id_organ');
					$sector = $form->getValue('id_sector');
					$date_created = date('Y-m-d');

					$sectorsDbTable = new Admin_Model_Sectors();

					$organsDbTable = new Admin_Model_Organs();
					$organ = $organsDbTable->getOrganByName($organ);

					$id_organ = null;
					if (!$organ['id']) {
						$this->view->priorityMessenger(sprintf('Não foi possível localizar o orgão selecionado "%s"', $form->getValue('id_organ')), 'error');
						return false;
					} else {
						$id_organ = $organ['id'];
						$form->getElement('id_organ')->setValue($id_organ);
					}

					$id_sector = null;
					if (($form->getValue('id_sector'))) {
						$sector = substr($sector,strpos($sector,'-')+1,strlen($sector));
						echo $sector;
						$sector = $sectorsDbTable->getSectorByName($sector);
						if (!$sector['id']) {
							$this->view->priorityMessenger(sprintf('Não foi possível localizar o setor selecionado "%s"', $form->getValue('id_sector')), 'error');
							return false;
						} else {
							$id_sector = $sector['id'];
							$form->getElement('id_sector')->setValue($id_sector);
						}
					}

					if (!$sectorsDbTable->isUniqueSectorName($name)) {
						$this->view->priorityMessenger(sprintf('Já exite um setor cadastrado com o nome "%s"', $name), 'error');
						return false;
					}

					if (!$sectorsDbTable->isUniqueSectorAcronym($acronym)) {
						$this->view->priorityMessenger(sprintf('Já exite um setor cadastrado a sigla "%s"', $acronym), 'error');
						return false;
					}

					$sectorsDbTable->addSector($name, $acronym, $id_organ, $id_sector, $date_created);

					$this->view->priorityMessenger(sprintf('Setor cadastrado "%s (%s)"', $name, $acronym), 'sucess');

					$this->_helper->redirector('list', 'sector', 'admin');
				} else {
					$form->populate($this->getRequest()->getPost());
				}
			}

		} catch (Exception $ex) {
			return false;
		}
	}

	public function updateAction() {
		$this->view->pageTitle = 'Editar setor';

		$form = new Admin_Form_SectorAdd();
		$request = $this->getRequest();

		try {

			$id = $this->_getParam('id', 0);

			if (!is_null($id)) {
				$sectorsDbTable = new Admin_Model_Sectors();
				$select = $sectorsDbTable->getSectorById($id);

				if (false === $select) {
					$this->view->priorityMessenger(sprintf('Nenhum orgão localizado com id "%s"', $id), 'error');

				} else {
					$this->view->form = $form;
					$form->populate($select);
					$this->view->data = $select;

					if ($request->isPost()) {
						if ($form->isValid($request->getPost())) {
							$name = $form->getValue('name');
							$acronym = $form->getValue('acronym');
							$organ = $form->getValue('id_organ');
							$sector = $form->getValue('id_sector');

							$organsDbTable = new Admin_Model_Organs();
							$organ = $organsDbTable->getOrganByName($organ);

							$id_organ = null;
							if (!$organ['id']) {
								$this->view->priorityMessenger(sprintf('Não foi possível localizar o orgão selecionado "%s"', $form->getValue('id_organ')), 'error');
								return false;
							} else {
								$id_organ = $organ['id'];
								$form->getElement('id_organ')->setValue($id_organ);
							}

							$id_sector = null;
							if (($form->getValue('id_sector'))) {
								$sector = substr($sector,strpos($sector,'-')+1,strlen($sector));
								$sector = $sectorsDbTable->getSectorByName($sector);
								if (!$sector['id']) {
									$this->view->priorityMessenger(sprintf('Não foi possível localizar o setor selecionado "%s"', $form->getValue('id_sector')), 'error');
									return false;
								} else {
									$id_sector = $sector['id'];
									$form->getElement('id_sector')->setValue($id_sector);
								}
							}

							$sectorsDbTable->updateSector($id, $name, $acronym, $id_organ, $id_sector);

							$this->view->priorityMessenger(sprintf('Orgão "%s (%s)" editado com sucesso', $select['name'], $select['acronym']), 'sucess');

							$this->_helper->redirector('list', 'sector', 'admin');

						} else {
							$form->populate($this->getRequest()->getPost());
						}
					}
				}

			}
		} catch (Exception $ex) {
			return false;
		}
	}

	public function deleteAction() {
		$this->view->pageTitle = 'Excluir setor';

		$form = new Admin_Form_DeleteConfirm();
		$request = $this->getRequest();

		$id = $this->_getParam('id', 0);
		if (!is_null($id)) {
			$sectorsDbTable = new Admin_Model_Sectors();
			$select = $sectorsDbTable->getSectorById($id);

			if (false === $select) {
				$this->view->priorityMessenger(sprintf('Nenhum setor localizado com id "%s"', $id), 'error');

			} else {
				$this->view->data = $select;

				if ($request->isPost()) {
					if ($form->isValid($request->getPost())) {

						$respYes = $form->getValue('yes');
						$respNo = $form->getValue('no');

						if (!is_null($respYes)) {
							$this->view->priorityMessenger(sprintf('Setor excluido "%s (%s)"', $select['name'], $select['acronym']), 'sucess');
							$sectorsDbTable->deleteSector($id);
							$this->_helper->redirector('list', 'sector', 'admin');
						}

						if (!is_null($respNo)) {
							$this->_helper->redirector('list', 'sector', 'admin');
						}

					}
				}

				$this->view->form = $form;
			}
		}
	}

}